package com.xpp.yanpicturebackend.util;

import java.awt.*;

public class ColorSimilarUtils {

    public static double calculateSimilarity(Color color1, Color color2) {
        // 颜色格式转换
        int r1 = color1.getRed();
        int g1 = color1.getGreen();
        int b1 = color1.getBlue();

        int r2 = color2.getRed();
        int g2 = color2.getGreen();
        int b2 = color2.getBlue();

        //计算欧式距离
        double distance = Math.sqrt(Math.pow(r2 - r1, 2) + Math.pow(g2 - g1, 2) + Math.pow(b2 - b1, 2));

        // 计算相似度
        return 1 - distance / Math.sqrt(3 * 255 * 255);
    }

    public static double calculateSimilarity(String hexColor1, String hexColor2) {
        Color color1 = Color.decode(hexColor1);
        Color color2 = Color.decode(hexColor2);
        return calculateSimilarity(color1, color2);
    }

    public static void main(String[] args) {
        String hexColor1 = "#FF0000";
        String hexColor2 = "#00FF00";
        double similarity = calculateSimilarity(hexColor1, hexColor2);
        System.out.println("相似度：" + similarity);
    }
}
